package com.lvmama.rhino.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.mapperDw.StockMapper;
import com.lvmama.rhino.pojo.StockDetail;
import com.lvmama.rhino.pojo.StockDetailQuery;
import com.lvmama.rhino.service.StockService;
import com.lvmama.rhino.utils.Constants;
import com.lvmama.rhino.utils.JSONConversionUtil;
import com.lvmama.rhino.utils.MD5Utils;
import com.lvmama.rhino.utils.PageInfoUtil;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

import java.util.*;

/**
 * @Author:caoyong
 * @Description:
 * @Date：2018/5/16
 */
@Service("stockService")
public class StockServiceImpl implements StockService {
    private Logger log = Logger.getLogger(StockServiceImpl.class);

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private JedisCluster jedisCluster;

    @Override
    public List<StockDetail> selectStockDetail(StockDetailQuery query) {
        log.info("selectStockDetail start, query:" + query);
        try {
            return stockMapper.selectStockDetail(query);
        } catch (Exception e) {
            log.error("selectStockDetail error:" + e.getMessage(), e);
        }
        log.info("selectStockDetail end.");
        return null;
    }

    @Override
    public List<StockDetail> selectStockDetailRedis(StockDetailQuery query) {
        log.info("selectStockDetail start, query:" + query);
        String key = MD5Utils.encode(query.toString());
        if (!jedisCluster.exists(key)){
            List<StockDetail> list = stockMapper.selectStockDetail(query);
            jedisCluster.set(key, JSONConversionUtil.objToString(list));
            jedisCluster.expire(key,Constants.TIMEOUT);
        }
        String result = jedisCluster.get(key);
        return JSONConversionUtil.stringToList(result,StockDetail.class);
    }

    @Override
    public PageInfo<StockDetail> selectStockDetailPage(StockDetailQuery query) {
        log.info("selectStockDetailPage start, query:" + ToStringBuilder.reflectionToString(query, ToStringStyle.DEFAULT_STYLE));
        //设置分页
        List<StockDetail> stockDetails = null;
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        try {
            stockDetails = stockMapper.selectStockDetail(query);
        } catch (Exception e) {
            log.error("selectStockDetailPage error:" + e.getMessage(), e);
        }
        log.info("selectStockDetailPage end.");
        return new PageInfo<>(stockDetails);
    }

    @Override
    public PageInfoUtil<StockDetail> selectStockDetailPageByRedis(StockDetailQuery query, int pageSize){
        log.info("selectStockDetailPageByRedis start, query:" + ToStringBuilder.reflectionToString(query, ToStringStyle.DEFAULT_STYLE));
        String key = MD5Utils.encode(query.toString());
        long total = jedisCluster.zcard(key);
        int pageStart = query.getCurrentPage() == 0 ? 0 : (query.getCurrentPage() - 1) * pageSize;
        int pageEnd = pageStart + pageSize - 1;
        //如果total 小于0 表示没有缓存重新加载
        if (total <= 0){
            List<StockDetail>  stockDetails = stockMapper.selectStockDetail(query);
            Map<String, Double> redisMap = new HashMap<>();
            if (stockDetails != null && stockDetails.size() > 0){
                double i = 0D;
                for (StockDetail stockDetail : stockDetails){
                    if (stockDetail == null){
                        continue;
                    }
                    String result = JSONConversionUtil.objToString(stockDetail);
                    redisMap.put(result,i);
                    i ++;
                }
            }
            total = redisMap.size();
            jedisCluster.zadd(key, redisMap);
            jedisCluster.expire(key,Constants.TIMEOUT);

        }
        Set<String> resultSet = jedisCluster.zrange(key, pageStart, pageEnd);
        List<StockDetail> list =  new ArrayList<>();

        if (resultSet != null && resultSet.size() > 0){
            for (String detail : resultSet){
                StockDetail stockDetail = JSONConversionUtil.stringToObj(detail,StockDetail.class);
                if (stockDetail != null){
                    list.add(stockDetail);
                }
            }
        }
        log.info("selectStockDetailPageByRedis end.");
        return  new PageInfoUtil(list, query.getCurrentPage(), pageSize, total, pageStart, pageEnd);
    }
}
